package main

import "fmt"

func deleteAndEarn(nums []int) int {

	maxVal := 0
	for _, val := range nums {
		maxVal = max(maxVal, val)
	}
	aa := make([]int, maxVal+1)
	for _, val := range nums {
		aa[val] += val
	}

	bb := make([]int, maxVal+1)

	for ii := range bb {

		if ii == 0 {
			bb[ii] = aa[ii]
		} else if ii == 1 {
			bb[ii] = max(aa[ii], aa[ii-1])
		} else {
			bb[ii] = max(aa[ii]+bb[ii-2], bb[ii-1])
		}

	}

	return bb[len(bb)-1]
}

/////////
func rob(nums []int) int {

	bb := make([]int, len(nums))

	for ii := range bb {

		if ii == 0 {
			bb[ii] = nums[ii]
		} else if ii == 1 {
			bb[ii] = max(nums[ii], nums[ii-1])
		} else {
			bb[ii] = max(nums[ii]+bb[ii-2], bb[ii-1])
		}

	}

	return bb[len(bb)-1]
}

///////////////
func numIslands(grid [][]byte) int {
	ret := 0

	for ii := range grid {
		for jj := range grid[ii] {
			if grid[ii][jj] == '1' {
				infect(ii, jj, grid)
				ret++
			}
		}

	}

	return ret
}

func infect(ii, jj int, grid [][]byte) {

	if ii < 0 || ii >= len(grid) || jj < 0 || jj >= len(grid[0]) || grid[ii][jj] == '0' {
		return
	}
	grid[ii][jj] = '0'

	infect(ii+1, jj, grid)
	infect(ii-1, jj, grid)
	infect(ii, jj+1, grid)
	infect(ii, jj-1, grid)
}

//////////////
// https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
func min(a, b int) (int, int) {
	if a <= b {
		return a, 0
	}
	return b, 1
}
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	// fmt.Println("", len(nums1), len(nums2))

	sortNums := make([]int, 0)
	ii, jj := 0, 0

	for len(nums1)-1 >= ii && len(nums2)-1 >= jj {
		// fmt.Println(" ii, jj", ii, jj)
		if nums1[ii] < nums2[jj] {
			sortNums = append(sortNums, nums1[ii])
			ii++
		} else {
			sortNums = append(sortNums, nums2[jj])
			jj++
		}
	}
	// fmt.Println(" ii, jj", ii, jj)

	if len(nums1)-1 < ii {
		sortNums = append(sortNums, nums2[jj:]...)
	} else if len(nums2)-1 < jj {
		sortNums = append(sortNums, nums1[ii:]...)
	}

	// fmt.Println("sortNums", g.Export(sortNums))
	Len := len(sortNums) / 2
	if len(sortNums)%2 == 0 {
		return float64(sortNums[Len-1]+sortNums[Len]) / 2
	} else {
		return float64(sortNums[Len])
	}
}

//////
func isMatch(s string, p string) bool {
	return true
}

/////////////////

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}
func lengthOfLongestSubstring(s string) int {
	if s == "" {
		return 0
	}
	ret := 0
	var aa = map[byte]int{}
	ii, jj := 0, 0
	for {
		// fmt.Println(jj, s[jj])
		if _, ok := aa[s[jj]]; ok {

			for ; s[ii] != s[jj]; ii++ {
				delete(aa, s[ii])
			}
			ii++
			jj++
		} else {
			aa[s[jj]] = 1
			ret = max(len(aa), ret)
			jj++
		}
		// fmt.Println("ii,jj", ii, jj, aa)
		if jj >= len(s) {
			break
		}

	}
	return ret
}

//////////////////

func main() {

	// findMedianSortedArrays([]int{1, 3}, []int{2})
	// a.Push(0)
	// res := lengthOfLongestSubstring("aab")
	// res := lengthOfLongestSubstring("abcabcbb")
	res := lengthOfLongestSubstring("aabaab!bb")
	fmt.Println("res", res)
}

//////////////////
